package com.cyq.dascma.util;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * request 工具类
 * dingwen
 * 2021/3/30 22:57
 **/
public class RequestUtil {
    /**
     * 获取request中的参数集合转Map
     *
     * @param request 请求
     * @return params
     */
    public static Map<String, Object> getParameterMap(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>();
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = paramNames.nextElement();
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() != 0 && StringUtils.hasText(paramValue)) {
                    params.put(paramName, paramValue);
                }
            }
        }
        return params;
    }
}
